package com.google.android.apps.wakemewhere;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import com.google.android.maps.GeoPoint;

// This class should use the Geocoder class but falls victim to:
// http://code.google.com/p/android/issues/detail?id=8816
public class AddressResolver {

	private final Geocoder geocoder;
	private final Logger logger = Logger.getLogger(AddressResolver.class.getName());

	public AddressResolver(Context context) {
		geocoder = new Geocoder(context);
	}
	
	public Address getAddress(GeoPoint geoPoint) {
		try {
			List<Address> addressesFound = geocoder.getFromLocation(geoPoint.getLatitudeE6() / 1E6, geoPoint.getLongitudeE6() / 1E6, 1);
			if (addressesFound.isEmpty()) {
				logger.info("No address returned by geocoder");
				return null;
			}
			return addressesFound.get(0);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		
		//String geocodeUriString = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=" + 
		//	latLngString(geoPoint);
		//Uri geocodeUri = new Uri.Builder().(geocodeUriString);
	}		
	
	private static String latLngString(GeoPoint geoPoint) {
		return "" + (geoPoint.getLatitudeE6() / 1E6) + "," + 
			        (geoPoint.getLongitudeE6() / 1E6);
	}
}
